﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.MailClient.Win.Forms;
using DevExpress.MailClient.Win.Forms.Campagne;
using DevExpress.XtraEditors;
using Zero.DataLayer;
using Zero.DataLayer.Repository;
using Zero.POCO.Compagne;
using Zero.POCO.Enum.Helper.Support;
using Zero.POCO.Helper.Support;
using Zero.POCO.Support;
using Zero.Presenter;
using Zero.Presenter.Donnees;
using TypeMedia = Zero.POCO.Compagne.TypeMedia;

namespace DevExpress.MailClient.Win.Modules.Donnees
{
    public partial class Plans : BaseModule, IPlanView
    {
        private ModelViewPlan modelViewPlan;
        public List<ModelViewPlan> ModelViewPlans { get; set; }
        public List<TarifTv> TarifTvs { get; set; }
        public List<TarifRd> TarifRds { get; set; }
        public List<TarifPr> TarifPrs { get; set; }
        public IEnumerable<SupportTV> SupportTvs { get; set; }
        public IEnumerable<SupportRadio> SupportRds { get; set; }
        public IEnumerable<SupportPresse> SupportPrs { get; set; }

        private PlanPresenter planPresenter;

        public Plans()
        {
            InitializeComponent();
            planPresenter = new PlanPresenter(this, new PlanTvRepository(), new PlanRdRepository(),
                new PlanPrRepository(), new SupportPresseRepository(), new SupportRadioRepository(),
                new SupportTvRepository(), new TarifTVRepository(), new TarifRdRepository(), new TarifPrRepository());
            planPresenter.Display();


            gcPlan.DataSource = ModelViewPlans;
            gvPlan.Columns[3].Visible = false;

            if (ModelViewPlans.Any())
            {
                modelViewPlan = ModelViewPlans.FirstOrDefault();

                if (modelViewPlan != null) gcMapTarif.DataSource = modelViewPlan.MapSupportTarifs;

                gvMapTarif.Columns[0].Visible = false;
                gvMapTarif.Columns[1].FieldName = "Support.Name";
                gvMapTarif.Columns[3].Visible = false;
                gvMapTarif.Columns[4].Visible = false;
                gvMapTarif.Columns[5].Visible = false;

                gvMapTarif.Columns[0].Visible = false;
            }
    }


        protected internal override void ButtonClick(string tag)
        {
            switch (tag)
            {

                case TagResources.NewPlan:
                    CreateNewPlan();
                    break;
            }
        }


        private void CreateNewPlan()
        {
            var campagne = new ModelViewPlan();
            //message.MailType = MailType.Draft;
            EditMessage(campagne, true, null);
        }

        private void EditMessage(ModelViewPlan campagne, bool newObject, string caption)
        {
            Cursor.Current = Cursors.WaitCursor;
            var form = new frmEditPlan(campagne, newObject, caption);
            //form.Load += OnEditMailFormLoad;
            //form.FormClosed += OnEditMailFormClosed;
            //form.Location = new Point(OwnerForm.Left + (OwnerForm.Width - form.Width)/2,OwnerForm.Top + (OwnerForm.Height - form.Height)/2);
            form.ShowDialog(this);
            form.TopMost = true;
            if (form.DialogResult == DialogResult.Yes)
            {
                planPresenter.WriteChanges(form.sourcePlan);
                planPresenter.Display();
                gcPlan.DataSource = ModelViewPlans;
            }

            Cursor.Current = Cursors.Default;
        }



        private void bbiAddMapTarifSupport_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {
            IEnumerable<String> listeUsed=modelViewPlan.MapSupportTarifs.Select(liste => liste.Support.Name);

            var Media = modelViewPlan.TypeMedia;
            List<string> listsupport=new List<string>();
            switch (Media)
            {
                case TypeMedia.TV:
                {
                     listsupport=SupportTvs.Where( sup=>!listeUsed.Contains((string)sup.Libelle)).Select(ed=>ed.Libelle).ToList();

                    break;
                }
                    case TypeMedia.RD:
                {
                    listsupport = SupportTvs.Where(sup => !listeUsed.Contains((string)sup.Libelle)).Select(ed => ed.Libelle).ToList();

                    break;
                }
                    case TypeMedia.PR:
                {
                    listsupport = SupportPrs.Where(sup => !listeUsed.Contains((string)sup.Libelle)).Select(ed => ed.Libelle).ToList();

                    break;
                }
            }

            Cursor.Current = Cursors.WaitCursor;
            var form = new EditMapTarif(null, true, TarifTvs,TarifRds,TarifPrs,listsupport,Media);
            // form.Load += OnEditMailFormLoad;
            // form.FormClosed += OnEditMailFormClosed;
            form.Location = new Point(Left + (Width - form.Width) / 2, Top + (Height - form.Height) / 2);

            form.Activate();
            form.ShowDialog();

            if (form.DialogResult == DialogResult.OK)
            {
                

                switch (Media)
                {
                    case TypeMedia.TV:
                    {
                        form.MapSupportTarif.Support.id =
                            SupportTvs.First(sup => sup.Libelle == form.MapSupportTarif.Support.Name).id;
                        break;
                    }
                        case TypeMedia.RD:
                    {
                        form.MapSupportTarif.Support.id =
                            SupportRds.First(sup => sup.Libelle == form.MapSupportTarif.Support.Name).id;
                        break;
                    }
                        case TypeMedia.PR:
                    {
                        form.MapSupportTarif.Support.id =
                            SupportPrs.First(sup => sup.Libelle == form.MapSupportTarif.Support.Name).id;
                        break;
                    }
                }
                modelViewPlan.MapSupportTarifs.Add(form.MapSupportTarif);
                planPresenter.WriteChanges(modelViewPlan);
                planPresenter.Display();
            }



            Cursor.Current = Cursors.Default;
        }

        private void bbiDeleteMap_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {

        }

        private void gvPlan_FocusedRowChanged(object sender, XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle >= 0)
            {

                modelViewPlan = ((ModelViewPlan) gvPlan.GetRow(e.FocusedRowHandle));

                if (modelViewPlan.MapSupportTarifs != null)
                {
                    gcMapTarif.DataSource = modelViewPlan.MapSupportTarifs;
                    
                    gvMapTarif.Columns[0].Visible = false;
                    gvMapTarif.Columns[1].FieldName = "Support.Name";
                    gvMapTarif.Columns[3].Visible = false;
                    gvMapTarif.Columns[4].Visible = false;
                    gvMapTarif.Columns[5].Visible = false;
                }
                else
                {
                    gcMapTarif.DataSource = new List<ModelViewMapTarif>();
                    gvMapTarif.Columns[0].Visible = false;
                    gvMapTarif.Columns[1].FieldName = "Support.Name";
                    gvMapTarif.Columns[3].Visible = false;
                    gvMapTarif.Columns[4].Visible = false;
                    gvMapTarif.Columns[5].Visible = false;

                }
            }
        }
    }
}
